package com.lagou.controllers;

import com.lagou.service.HelloService;
import com.lagou.service.HelloService2;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author zhangfd
 * @Date 2020/5/11 17:24
 * @Version 1.0
 * @Describe：
 */
@RestController
@RequestMapping("/lagou")
public class TestController {

    @Reference
    private HelloService helloService;

    @Reference
    private HelloService2 helloService2;

    @RequestMapping(value="/test")
    public String test1(@RequestParam(value="name") String name, HttpServletRequest request) {
        String ip =  request.getRemoteHost();
        RpcContext.getContext().setAttachment("ip", ip);
        String re = helloService.sayHello(name,0);
        RpcContext.getContext().setAttachment("ip", ip);
         re = re + helloService2.sayHello(name,0);
         return "dubbo: "+ re;
    }


    public  String getRemoteAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
